package com.iv.durian.cloud.api;

import com.iv.durian.cloud.Content;
import com.iv.durian.cloud.Coverage;
import com.iv.durian.cloud.JsonResult;
import com.iv.durian.cloud.api.service.CoverageService;
import com.iv.durian.cloud.vo.ContentVO;
import com.iv.durian.cloud.vo.CoverageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by LiuLin on 2018/6/30 0030.
 */
@RestController
@RequestMapping("/api/coverage")
@Slf4j
@Api(value = "CoverageApi", description = "CoverageApi", tags = {"coverage"})
public class CoverageApi {
    @Autowired
    private CoverageService coverageService;

    @ApiOperation(value = "资讯列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JsonResult<List<CoverageVO>> list(
            @ApiParam(required = true, name = "type", value = "类型") @RequestParam("type") Integer type
    ) {
        JsonResult r = new JsonResult();
        List<Coverage> coverageList = coverageService.list(type);
        List<CoverageVO> coverageVOList =  coverageList.stream().map(coverage -> {
            CoverageVO coverageVO = CoverageVO.builder()
                    .createTime(coverage.getCreateTime())
                    .id(coverage.getId())
                    .image(coverage.getImage())
                    .original(coverage.getOriginal())
                    .sequence(coverage.getSequence())
                    .title(coverage.getTitle())
                    .type(coverage.getType())
                    .updateTime(coverage.getUpdateTime())
                    .build();
            return coverageVO;
        }).collect(Collectors.toList());
        r.ok(coverageVOList);
        return r;
    }

    @ApiOperation(value = "获取资讯信息")
    @RequestMapping(value = "/get/{coverage_id}", method = RequestMethod.GET)
    public JsonResult<CoverageVO> get(
            @ApiParam(required = true, name = "coverage_id", value = "资讯ID") @PathVariable("coverage_id") Integer coverageId) {
        JsonResult r = new JsonResult();
        Coverage coverage = coverageService.get(coverageId);
        Assert.notNull(coverage,"资讯不存在");
        CoverageVO coverageVO = CoverageVO.builder()
                .createTime(coverage.getCreateTime())
                .id(coverage.getId())
                .image(coverage.getImage())
                .original(coverage.getOriginal())
                .sequence(coverage.getSequence())
                .title(coverage.getTitle())
                .type(coverage.getType())
                .updateTime(coverage.getUpdateTime())
                .build();
        r.ok(coverageVO);
        return r;
    }

    @ApiOperation(value = "新增资讯信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void add(
            @ApiParam(required = true, name = "coverage", value = "coverage对象") @RequestBody(required = true) Coverage coverage) {
        coverageService.add(coverage);
    }

    @ApiOperation(value = "删除资讯")
    @RequestMapping(value = "/remove/{coverage_id}", method = RequestMethod.GET)
    public int remove(
            @ApiParam(required = true, name = "coverage_id", value = "资讯id") @PathVariable("coverage_id") Integer coverageId) {
        return coverageService.remove(coverageId);
    }

    @ApiOperation(value = "计算资讯的数量")
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    public int count(
            @ApiParam(required = true, name = "type", value = "类型") @RequestParam("type") Integer type
    ) {
        return coverageService.count(type);
    }

    @ApiOperation(value = "修改资讯的信息")
    @RequestMapping(value = "/update/coverage/{id}", method = RequestMethod.POST)
    public void update(
            @ApiParam(required = true, name = "coverage", value = "资讯对象") @RequestBody Coverage coverage
    ) {
        coverageService.update(coverage);
    }

    @ApiOperation(value = "查询资讯的内容")
    @RequestMapping(value = "/get/content/{id}", method = RequestMethod.GET)
    public ContentVO getContent(
            @ApiParam(required = true, name = "id", value = "资讯ID") @PathVariable Integer id
    ) {
        Content content = coverageService.getContent(id);
        Assert.notNull(content,"资讯内容不存在");
        ContentVO contentVO = ContentVO.builder().content(content.getContent()).coverageId(content.getCoverageId()).build();
        return contentVO;
    }


    @ApiOperation(value = "修改资讯的内容")
    @RequestMapping(value = "/update/content", method = RequestMethod.POST)
    public void updateContent(
            @ApiParam(required = true, name = "content", value = "content对象") @RequestBody(required = true) Content content

    ) {
        coverageService.updateContent(content);
    }
}
